#include "BannerWnd.h"
#include "ui_BannerWnd.h"

#include "GlobalSignal.h"

#include <QTimer>
#include <QDateTime>
#include <QDate>
#include <QDebug>

#include "MainApp.h"

#define DATETIME_FORMAT          "yyyy-MM-dd hh:mm:ss"

using namespace zl;

BannerWnd::BannerWnd(QWidget *parent) :
    QWidget(parent)
  , ui(new Ui::BannerWnd)
  , m_timer(new QTimer(this))
  , m_timer_cnt(0)
  , m_timer_flag(false)
  , m_backend_page(EBP_Unknow)
{
    ui->setupUi(this);

    this->setFixedSize(this->width(), this->height());

    // init
    ui->lb_title->installEventFilter(this);

    QObject::connect(GlobalSignal::getInstance(), &GlobalSignal::switchLanguage, this, &BannerWnd::lang_change);

    // show date time
    auto timer = new QTimer(this);
    timer->start(1000);
    connect(timer, &QTimer::timeout, this, [&]() {
        ui->lb_date_time->setText(QDateTime::currentDateTime().toString(DATETIME_FORMAT));
        ui->lb_day_time->setText(CurrentDayOfWeek());
    });
}

BannerWnd::~BannerWnd()
{
    QObject::disconnect(GlobalSignal::getInstance(), &GlobalSignal::switchLanguage, this, &BannerWnd::lang_change);

    if (m_timer->isActive()) m_timer->stop();

    delete ui;
}

void BannerWnd::lang_change()
{
    if (ui) ui->retranslateUi(this);

    set_backend_title(m_backend_page);
}

QString BannerWnd::CurrentDayOfWeek()
{
    QString dayOfWeek = "";

    int32_t index = QDate::currentDate().dayOfWeek();

    switch (index) {
    case 1:
       dayOfWeek = tr("Mon");
        break;
    case 2:
       dayOfWeek = tr("Tue");
        break;
    case 3:
       dayOfWeek = tr("Wed");
        break;
    case 4:
       dayOfWeek = tr("Thu");
        break;
    case 5:
       dayOfWeek = tr("Fri");
        break;
    case 6:
       dayOfWeek = tr("Sat");
        break;
    case 7:
       dayOfWeek = tr("Sun");
        break;
    default:
        break;
    }

    return dayOfWeek;
}

bool BannerWnd::eventFilter(QObject * watched, QEvent * event)
{
    if (qobject_cast<QLabel*>(watched) == ui->lb_title)
    {
        if (event->type() == QEvent::MouseButtonDblClick)
        {
            emit enterBackend();
        }
    }

    return QWidget::eventFilter(watched, event);
}

void BannerWnd::Show_home_banner()
{
    toggle_show_top_head(true);
}

void BannerWnd::Show_backend_select_banner()
{
    ui->stackedWidget_banner->setCurrentWidget(ui->page_back_sel);

    toggle_show_top_head(false);
}

void BannerWnd::Show_backend_page_banner()
{
    ui->stackedWidget_banner->setCurrentWidget(ui->page_back);

    toggle_show_top_head(false);
}

void BannerWnd::SetCabinetInfo(QString name, QString title)
{
    Q_UNUSED(name);

    ui->lb_title->setText(title);
}

void BannerWnd::SetBannerWorkFlow(zl::EWF workFlow)
{
    if (workFlow == EWF_Home)
        Show_home_banner();
}

void BannerWnd::SetBannerBackendModule(zl::EBP backendPage)
{
    m_backend_page = backendPage;

    set_backend_title(backendPage);
}

void BannerWnd::set_backend_title(zl::EBP backendPage)
{
    QString backend_title = "";

    switch (backendPage)
    {
    case zl::EBP_Select_page:
        break;
    case zl::EBP_Record_page:
        backend_title = tr("Query Test Record");
        break;
    case zl::EBP_Advance_page:
        backend_title = tr("Setting");
        break;
    default:
        break;
    }

    ui->lb_backend_title->setText(backend_title);
}

void BannerWnd::toggle_show_top_head(bool show)
{
    ui->wnd_top_info->setVisible(show);
    ui->stackedWidget_banner->setVisible(!show);
}

void BannerWnd::on_btn_quit_bk_clicked()
{
    emit backToHome();
}

void BannerWnd::on_btn_exit_clicked()
{
    emit exitApp();
}

void BannerWnd::on_btn_bk_back_clicked()
{
    emit GlobalSignal::getInstance()->backend_page_change(zl::EBP_Select_page);
}
